package com.example.forum.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.forum.MyQuery;
import com.example.forum.commons.tools.StringUtil;
import com.example.forum.commons.tools.Transfer.DataTransfer;
import com.example.forum.entity.TAppuser;
import com.example.forum.entity.TTheme;
import com.example.forum.entity.TThemecontent;
import com.example.forum.entity.TThemereply;
import com.example.forum.service.ITAppuserService;
import com.example.forum.service.ITThemeService;
import com.example.forum.service.ITThemecontentService;
import com.example.forum.service.ITThemereplyService;
import com.example.forum.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author why
 * @since 2020-04-22
 */
@RestController
@RequestMapping("Theme")
public class TThemeController extends BaseController {

    @Autowired
    ITThemeService themeService;

    @Autowired
    ITThemereplyService itThemereplyService;

    @Autowired
    ITAppuserService appuserService;

    @Autowired
    ITThemecontentService themecontentService;

    @GetMapping("pcIndex")
    public JsonResult pcIndex(@RequestParam(defaultValue = "1") Integer pageIndex,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String TitleOrContent,
                              Integer here,
                              @RequestParam(defaultValue = "null") String ThemeType) {
        PageDto pageDto = null;
        Map<Object, Object> map = new HashMap<>();
        map.put("current", (pageIndex - 1) * pageSize);
        map.put("pageSize", pageSize);
        map.put("TitleOrContent", TitleOrContent);
        map.put("type", "null".equals(ThemeType) ? -1 : ThemeType);
        if (here == 1) {//pc用户
            pageDto = themeService.getThemeReplyInfo(map);
        } else {//app用户
            TAppuser appuser = appuserService.getById(currentUserId());
            if (appuser != null) {
                if (appuser.getAppointstatus() == 2) {
                    return new JsonResult(HttpStatus.FORBIDDEN, "权限已撤销", null);
                } else if (appuser.getAppointstatus() == 0) {
                    return new JsonResult(HttpStatus.FORBIDDEN, "权限不足", null);
                }
                pageDto = themeService.getThemeReplyInfo(map);
            }
        }
        return new JsonResult(HttpStatus.OK, "成功", pageDto);
    }

    @GetMapping("GetMySelfThemeOrThemeReply")
    public JsonResult getMySelfThemeOrThemeReply(@RequestParam(defaultValue = "1") Integer PageIndex,
                                                 @RequestParam(defaultValue = "10") Integer PageSize,
                                                 String userId, @RequestParam(defaultValue = "2") Integer Type) {
        if (StringUtil.isEmpty(userId)) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "用户id不能为空", null);
        }
        if (appuserService.getById(userId) == null) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "用户不存在", null);
        }
        HashMap<Object, Object> map = new HashMap<>();
        map.put("type", Type);
        PageIndex = PageIndex;

        map.put("current", (PageIndex - 1) * PageSize);
        map.put("pageSize", PageSize);
        map.put("userid", userId);
        return new JsonResult(HttpStatus.OK, "成功", themeService.queryMyThemeOrReply(map));
    }

    /**
     * 话题展现
     *
     * @param themeId
     * @param type
     * @param isPass
     * @return
     */
    @PostMapping("UpdateThemeStatus")
    public JsonResult updateThemeStatus(String themeId, Integer type, boolean isPass) {
        TTheme theme = new TTheme();
        theme.setId(themeId);
        theme.setStatus(isPass ? 2 : 3);
        if (!isPass) {
            //取消推荐
            theme.setIsrecommend(false);
            TTheme tTheme = themeService.getById(themeId);
            if (tTheme == null) {
                return new JsonResult(HttpStatus.NOT_FOUND, "该主题不存在", null);
            }
            //增加预警数
            appuserService.addUserWarningCount(tTheme.getCreatoruserid());
        }
        if (themeService.updateById(theme)) {
            return new JsonResult(HttpStatus.OK, "更新成功", null);
        }
        return new JsonResult(HttpStatus.NOT_FOUND, "更新失败", null);
    }

    /**
     * 话题推荐
     *
     * @return
     */
    @PostMapping("UpdateThemeRecommend")
    public JsonResult updateThemeRecommend(String themeId, Integer type, boolean IsRecommend) {
        TTheme theme = new TTheme();
        theme.setId(themeId);
        theme.setIsrecommend(IsRecommend);
        if (themeService.updateById(theme)) {
            return new JsonResult(HttpStatus.OK, "更新成功", null);
        }
        return new JsonResult(HttpStatus.NOT_FOUND, "该主题不存在", null);
    }

    @PostMapping("delete")
    public JsonResult delete(String themeId, Integer type) {
        TTheme theme = new TTheme();
        theme.setId(themeId);
        theme.setIsdeleted(true);
        if (themeService.updateById(theme)) {
            return new JsonResult(HttpStatus.OK, "删除成功", null);
        }
        return new JsonResult(HttpStatus.NOT_FOUND, "该主题不存在", null);
    }

    /**
     * 获取当前位置所有主题
     * @return
     */
    @GetMapping("GetThemesByAreaId")
    public JsonResult getThemesByAreaId(@RequestParam(defaultValue = "1") Integer pageIndex,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        String blockId,String areaId){
        TAppuser appuser = appuserService.getById(currentUserId());
        if(appuser==null){
            return new JsonResult(HttpStatus.NOT_FOUND, "用户不存在", null);
        }

        PageDto themesByAreaId = themeService.getThemesByAreaId((pageIndex - 1) * pageSize, pageSize, blockId, areaId, currentUserId());
        return new JsonResult(HttpStatus.OK,"",themesByAreaId);
    }

    /**
     * 主页推荐
     * @return
     */
    @GetMapping("HomeThemeRecommend")
    public JsonResult homeThemeRecommend(@RequestParam(defaultValue = "1") Integer pageIndex,
                                         @RequestParam(defaultValue = "10") Integer pageSize){
        TAppuser appuser = appuserService.getById(currentUserId());
        if(appuser==null){
            return new JsonResult(HttpStatus.NOT_FOUND, "用户不存在", null);
        }
        PageDto pageDto = themeService.homeThemeRecommend(appuser, (pageIndex - 1) * pageSize, pageSize
        );
        return new JsonResult(HttpStatus.OK,"成功",pageDto);
    }



    /**
     * 发布文章
     * @return
     */
    @PostMapping("add")
    public JsonResult add(@RequestBody ThemeModel themeModel){
        TAppuser appuser = appuserService.getById(currentUserId());
        if(appuser==null){
            return new JsonResult(HttpStatus.NOT_FOUND, "用户不存在", null);
        }else if(appuser.getIsprohibit()){
            return new JsonResult(HttpStatus.SEE_OTHER, "该用户已被禁用", null);
        }

        themeService.CreateTheme(themeModel,appuser.getId());
        return new JsonResult(HttpStatus.OK, "成功", null);
    }


    @GetMapping("GetThemesDetail")
    public JsonResult getThemesDetail(String themeId){
        TAppuser appuser = appuserService.getById(currentUserId());
        if (StringUtil.isEmpty(themeId)) {
            return new JsonResult(HttpStatus.FORBIDDEN, "主题id不能为空", null);
        }
        TTheme theme = themeService.getThemesDetail(themeId,appuser.getId());
        if (theme==null) {
            return new JsonResult(HttpStatus.NOT_FOUND, "主题不存在", null);
        }
        return new JsonResult(HttpStatus.OK,"成功",theme);
    }
}
